package br.net.woodstock.tickettoride.api.core.utils;

import java.util.regex.Pattern;

public class Filter {

	private Pattern	pattern;

	public Filter(final Pattern pattern) {
		super();
		this.pattern = pattern;
	}

	public boolean matches(final String text) {
		if (this.pattern.matcher(text).matches()) {
			return true;
		}
		return false;
	}

	public String getValue(final String text) {
		String strPattern = this.pattern.pattern();
		String endsWith = FilterHelper.ANY_PATTERN;
		if (strPattern.endsWith(endsWith)) {
			strPattern = strPattern.substring(0, strPattern.length() - endsWith.length());
		}
		String regex = strPattern;
		String out = text.replaceAll(regex, FilterHelper.REPLACEMENT);
		return out;
	}

	@Override
	public String toString() {
		return this.pattern.pattern();
	}

}
